package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.CommonRoute;
import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.mapper.ICommonRouteMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRoutePointMapper;
import com.enjoyor.soa.traffic.core.tsms.service.ICommonRouteService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

import oracle.spatial.geometry.JGeometry;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class CommonRouteService implements ICommonRouteService{

	@Autowired
	private ICommonRouteMapper iCommonRouteMapper;
	
	@Autowired
	private IRoutePointMapper iRoutePointMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Override
	public Page getCommonRoutePage(Page page, String commonRouteId, String commonRouteName, String routeStartPlace,
			String routeTargetPlace) {
		// TODO Auto-generated method stub
		Map<Object, Object> mapBean = new HashMap<>();
		mapBean.put("commonRouteId",commonRouteId);
		mapBean.put("commonRouteName",commonRouteName);
		mapBean.put("routeStartPlace",routeStartPlace);
		mapBean.put("routeTargetPlace",routeTargetPlace);
		page.setMapBean(mapBean);
		Page resultPage = iCommonRouteMapper.selectPageInfo(page);
		List<CommonRoute> routeInfoList = (List<CommonRoute>)resultPage.getResult();
		for(CommonRoute routeInfo : routeInfoList){
			String strGem = GeomtryHelper.getGeomToStringWithGson(routeInfo.getRouteGeo());
			routeInfo.setStrArea(strGem);
		}
		resultPage.setResult(routeInfoList);
		return resultPage;
	}

	@Override
	public List<CommonRoute> getCommonRouteList(String commonRouteId, String commonRouteName, String routeStartPlace,
			String routeTargetPlace) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("commonRouteId", commonRouteId);
		map.put("commonRouteName", commonRouteName);
		map.put("routeStartPlace", routeStartPlace);
		map.put("routeTargetPlace", routeTargetPlace);
		
		List<CommonRoute> commonRouteList = iCommonRouteMapper.getRouteInfoList(map);
		for(CommonRoute commonRoute : commonRouteList){
			String strGem = GeomtryHelper.getGeomToStringWithGson(commonRoute.getRouteGeo());
			commonRoute.setStrArea(strGem);
		}
		return iCommonRouteMapper.getRouteInfoList(map);
	}

	@Override
	public String insert(CommonRoute commonRoute,String personName) {
		// TODO Auto-generated method stub
		String commonRouteId = "CR"+ TimeHelper.getUniqueString();
		commonRoute.setCommonRouteId(commonRouteId);
		commonRoute.setCreatePerson(personName);
		commonRoute.setCreateTime(TimeHelper.dateToLongString());
		commonRoute.setIsDelete(false);
		commonRoute.setUpdateTime(TimeHelper.dateToLongString());
		iCommonRouteMapper.insert(commonRoute);
		List<RoutePoint> routePointList = commonRoute.getRoutePointList();
		int pointSortno = 1;
		for(RoutePoint routePoint : routePointList){
			routePoint.setPointId("RO"+ TimeHelper.getUniqueString());
			routePoint.setRouteId(commonRouteId);
			routePoint.setPointSortno(pointSortno);
			if(routePoint.getParkTime() > (Double)0.0) {
				routePoint.setPointTypeId(EnumDicPublic.ROUTE_STOP.getKey());
			}
			String pointTypeId = routePoint.getPointTypeId();
			if(EnumDicPublic.ROUTE_START.getKey().equals(pointTypeId)) {
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_START.getValue());
			}else if(EnumDicPublic.ROUTE_PATHWAY.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_PATHWAY.getValue());
			}else if(EnumDicPublic.ROUTE_STOP.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_STOP.getValue());
			}else if(EnumDicPublic.ROUTE_TARGET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_TARGET.getValue());
			}else if(EnumDicPublic.ROUTE_BAYONET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_BAYONET.getValue());
			}else if(EnumDicPublic.ROUTE_CROSS.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_CROSS.getValue());
			}else {
				return "false";
			}
			iRoutePointMapper.insert(routePoint);
			pointSortno++;
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加常备线路:{%s} 操作成功!", new Object[] { commonRoute.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_COMMON_ROUTE_MANAGEMENT.getMessage());
		return commonRouteId;
	}

	@Override
	public Boolean update(CommonRoute commonRoute,String personName) {
		// TODO Auto-generated method stub
		Boolean flag = true;
		String commonRouteId = commonRoute.getCommonRouteId();
		commonRoute.setUpdateTime(TimeHelper.dateToLongString());
		//更新线路基本信息；
		iCommonRouteMapper.updateByIdSelective(commonRoute);
		//删除旧关联点；
		iRoutePointMapper.deleteByRouteId(commonRouteId);
		//重新添加关联途径点；
		List<RoutePoint> routePointList = commonRoute.getRoutePointList();
		int pointSortno = 1;
		for(RoutePoint routePoint : routePointList){
			routePoint.setPointId("RO"+ TimeHelper.getUniqueString());
			routePoint.setRouteId(commonRouteId);
			routePoint.setPointSortno(pointSortno);
			if(routePoint.getParkTime() > (Double)0.0) {
				routePoint.setPointTypeId(EnumDicPublic.ROUTE_STOP.getKey());
			}
			String pointTypeId = routePoint.getPointTypeId();
			if(EnumDicPublic.ROUTE_START.getKey().equals(pointTypeId)) {
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_START.getValue());
			}else if(EnumDicPublic.ROUTE_PATHWAY.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_PATHWAY.getValue());
			}else if(EnumDicPublic.ROUTE_STOP.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_STOP.getValue());
			}else if(EnumDicPublic.ROUTE_TARGET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_TARGET.getValue());
			}else if(EnumDicPublic.ROUTE_BAYONET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_BAYONET.getValue());
			}else if(EnumDicPublic.ROUTE_CROSS.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_CROSS.getValue());
			}else {
				flag = false;
				return flag;
			}
			iRoutePointMapper.insert(routePoint);
			pointSortno++;
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改常备线路:{%s} 操作成功!", new Object[] { commonRoute.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_COMMON_ROUTE_MANAGEMENT.getMessage());
		return flag;
	}

	@Override
	public void delete(String commonRouteId,String personName) {
		// TODO Auto-generated method stub
		//删除常备线路关联的途径点信息；
		iRoutePointMapper.deleteByRouteId(commonRouteId);
		//删除常备线路信息；
		iCommonRouteMapper.deleteById(commonRouteId);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除常备线路:{%s} 操作成功!", new Object[] { commonRouteId }),
	                LogInfoDic.LOG_MODULAR_ENUM.TSMS_COMMON_ROUTE_MANAGEMENT.getMessage());
	}

}
